home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffe / native.c < prev    next >
C/C++ Source or Header  |  1996-02-18  |  3KB  |  151 lines

  1. /*
  2.  * native.c
  3.  * Handle native method calls.
  4.  *
  5.  * Copyright (c) 1996 Systems Architecture Research Centre,
  6.  *           City University, London, UK.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  12.  */
  13.  
  14. #define    DBG(s)
  15.  
  16. #include <stdio.h>
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <dlfcn.h>
  21. #include "gtypes.h"
  22. #include "classMethod.h"
  23. #include "native.h"
  24. #include "errors.h"
  25. #include "exception.h"
  26. #include "config.h"
  27.  
  28. /*
  29.  * Some version of dlsym need an underscore, some don't.
  30.  */
  31. #if defined(NEED_DYN_UNDERSTORE)
  32. #define    STUB_PREFIX        "_Kaffe_"
  33. #define    STUB_PREFIX_LEN        7
  34. #else
  35. #define    STUB_PREFIX        "Kaffe_"
  36. #define    STUB_PREFIX_LEN        6
  37. #endif
  38.  
  39. static void* libHandle[MAXLIBS];
  40. char libraryPath[MAXLIBPATH];
  41.  
  42. void
  43. initNative(void)
  44. {
  45.     char lib[MAXLIBPATH];
  46.     char* ptr;
  47.     char* nptr;
  48.  
  49.     ptr = getenv(LIBRARYPATH);
  50.     if (ptr == 0) {
  51.         fprintf(stderr, "No library path set.\n");
  52.         return;
  53.     }
  54.     strcpy(libraryPath, ptr);
  55.  
  56.     /* Find the default library */
  57.     for (ptr = libraryPath; ptr != 0; ptr = nptr) {
  58.         nptr = strchr(ptr, ':');
  59.         if (nptr == 0) {
  60.             strcpy(lib, ptr);
  61.         }
  62.         else {
  63.             strncpy(lib, ptr, nptr - ptr);
  64.             lib[nptr-ptr] = 0;
  65.             nptr++;
  66.         }
  67.         strcat(lib, "/");
  68.         strcat(lib, NATIVELIBRARY);
  69.  
  70.         if (loadNativeLibrary(lib) == 0) {
  71.             return;
  72.         }
  73.     }
  74.     fprintf(stderr, "Failed to locate native library ... aborting.\n");
  75.     fflush(stderr);
  76.     exit(1);
  77. }
  78.  
  79. int
  80. loadNativeLibrary(char* lib)
  81. {
  82.     int i;
  83.  
  84.     /* Find a library handle */
  85.     for (i = 0; i < MAXLIBS; i++) {
  86.         if (libHandle[i] == 0) {
  87.             goto open;
  88.         }
  89.     }
  90.     return (-1);
  91.  
  92.     /* Open the library */
  93.     open:
  94.     libHandle[i] = dlopen(lib, 1);
  95.     if (libHandle[i] == 0) {
  96.         return (-1);
  97.     }
  98.     return (0);
  99. }
  100.  
  101. void
  102. native(methods* m)
  103. {
  104.     char stub[MAXSTUBLEN];
  105.     char* ptr;
  106.     int i;
  107.     void* func;
  108.  
  109.     /* Construct the stub name */
  110.     strcpy(stub, STUB_PREFIX);
  111.     ptr = m->class->name;
  112.     for (i = STUB_PREFIX_LEN; *ptr != 0; ptr++, i++) {
  113.         if (*ptr == '/') {
  114.             stub[i] = '_';
  115.         }
  116.         else {
  117.             stub[i] = *ptr;
  118.         }
  119.     }
  120.     stub[i] = '_';
  121.     stub[i+1] = 0;
  122.     strcat(stub, m->pair->s1);
  123.     strcat(stub, "_stub");
  124.  
  125. DBG(    printf("Method = %s.%s%s\n", m->class->name, m->pair->s1, m->pair->s2);)
  126. DBG(    printf("Native stub = '%s'\n", stub);fflush(stdout);        )
  127.  
  128.     /* Find the native method */
  129.     for (i = 0; i < MAXLIBS && libHandle[i] != 0; i++) {
  130.         func = dlsym(libHandle[i], stub);
  131.         if (func != 0) {
  132.             /* Fill it in */
  133.             m->ncode = func;
  134.             return;
  135.         }
  136.     }
  137.     fprintf(stderr, "Failed to locate native function:\n\t%s.%s%s\n", m->class->name, m->pair->s1, m->pair->s2);
  138.     fflush(stderr);
  139.  
  140.     throwException(UnsatisfiedLinkError);
  141. }
  142.  
  143. /*
  144.  * Return the library path.
  145.  */
  146. char*
  147. getLibraryPath(void)
  148. {
  149.     return (libraryPath);
  150. }
  151.